package com.cn.ant.modules.user.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.modules.user.dao.UserFavoriteDao;
import com.cn.ant.modules.user.entity.UserFavorite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户收藏Service
 * @author 黄根华
 * @version 2015-10-25
 */
@Service
@Transactional(readOnly = true)
public class UserFavoriteService extends BaseService {

    @Autowired
	private UserFavoriteDao userFavoriteDao;

	public UserFavorite get(String id) {
		return userFavoriteDao.get(id);
	}
	
	public List<UserFavorite> findList(UserFavorite userFavorite) {
		return userFavoriteDao.findList(userFavorite);
	}
	
	public Page<UserFavorite> findPage(Page<UserFavorite> page, UserFavorite userFavorite) {
	    userFavorite.setPage(page);
        page.setList(userFavoriteDao.findList(userFavorite));
        return page;
	}

	/**
	 * 判断用户是否收藏相关信息
	 * @param type
	 * @param userId
	 * @param vid
	 * @param schoolCode
	 * @param schoolCodeSn
	 * @param majorCode
	 * @return
	 */
	public boolean isFav(String type, String userId, String vid, String schoolCode, String schoolCodeSn, String majorCode) {
		UserFavorite params = new UserFavorite();
		params.setType(type);
		params.setUserId(userId);
		params.setVolunteerId(vid);
		params.setSchoolCode(schoolCode);
		params.setSchoolCodeSn(schoolCodeSn);
		params.setMajorCode(majorCode);
		List<UserFavorite> favoriteList = userFavoriteDao.findList(params);
		if (favoriteList != null && favoriteList.size() > 0) {
			return true;
		}
		return false;
	}
	
	@Transactional(readOnly = false)
	public void save(UserFavorite userFavorite) {
	    if (userFavorite.getIsNewRecord()) {
            userFavorite.preInsert();
            userFavoriteDao.insert(userFavorite);
        } else {
            userFavorite.preUpdate();
            userFavoriteDao.update(userFavorite);
        }
	}
	
	@Transactional(readOnly = false)
	public void delete(UserFavorite userFavorite) {
		userFavoriteDao.delete(userFavorite);
	}


	@Transactional(readOnly = false)
	public void deleteByType(UserFavorite userFavorite) {
		userFavoriteDao.deleteByType(userFavorite);
	}
}